/**
 * @author yxw
 * date : 2013年11月18日 下午4:17:19 
 */
package com.tzinfo.tzt.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.tzinfo.tzt.config.AppDataConfig;
import com.tzinfo.tzt.domain.NewhouseList;
import com.tzinfo.tzt.domain.OldhouseList;
import com.tzinfo.tzt.domain.RenthouseList;
import com.tzinfo.tzt.util.DBHelper;

public class HouseCallService {

	Context context;

	public HouseCallService(Context context) {
		this.context = context;
	}

	
	public boolean deleteAll(int tab){
		return DBHelper.getInstance(context).deleteAll(AppDataConfig.CallHistoryTables[tab]);
	}
	public boolean deleteOldById(String id){
		int flag=DBHelper.getInstance(context).deleteById(AppDataConfig.CallHistoryTables[0],id);
		return flag>0?true:false;
	}
	public boolean deleteNewById(String id){
		int flag=DBHelper.getInstance(context).deleteById(AppDataConfig.CallHistoryTables[1],id);
		return flag>0?true:false;
	}
	public boolean deleteRentById(String id){
		int flag=DBHelper.getInstance(context).deleteById(AppDataConfig.CallHistoryTables[2],id);
		return flag>0?true:false;
	}
	
	
	public List<OldhouseList> getOldHouseList() {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[0],"time","desc");
		List<OldhouseList> list = new ArrayList<OldhouseList>();
		while (cursor.moveToNext()) {
			OldhouseList item = new OldhouseList();
			item.setID(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStyle(cursor.getString(cursor.getColumnIndex("style")));
			item.setSize(cursor.getString(cursor.getColumnIndex("size")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(item);
		}
		cursor.close();
		return list;
	}
	
	public OldhouseList getOldHouseById(String id) {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[0],id);
		OldhouseList item = null;
		if (cursor.moveToNext()) {
			item=new OldhouseList();
			item.setID(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStyle(cursor.getString(cursor.getColumnIndex("style")));
			item.setSize(cursor.getString(cursor.getColumnIndex("size")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
		}
		cursor.close();
		return item;
	}

	public boolean saveOldHouse(OldhouseList item) {
		ContentValues value = new ContentValues();
		value.put("_id", item.getID());
		value.put("name", item.getName());
		value.put("address", item.getAddress());
		value.put("logo", item.getLogo());
		value.put("style", item.getStyle());
		value.put("size", item.getSize());
		value.put("price", item.getPrice());
		value.put("lon", item.getLon());
		value.put("lat", item.getLat());

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("time", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(
				AppDataConfig.CallHistoryTables[0], value);
		return flag > 0 ? true : false;
	}

	public List<NewhouseList> getNewHouseList() {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[1],"time","desc");
		List<NewhouseList> list = new ArrayList<NewhouseList>();
		while (cursor.moveToNext()) {
			NewhouseList item = new NewhouseList();
			item.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			item.setAttr(cursor.getInt(cursor.getColumnIndex("attr")));
			item.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
			item.setPriceUnit(cursor.getString(cursor
					.getColumnIndex("priceUnit")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(item);
		}
		cursor.close();
		return list;
	}
	
	public NewhouseList getNewHouseById(String id) {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[1],id);
		NewhouseList item=null;
		if (cursor.moveToNext()) {
			item = new NewhouseList();
			item.setId(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStatus(cursor.getString(cursor.getColumnIndex("status")));
			item.setAttr(cursor.getInt(cursor.getColumnIndex("attr")));
			item.setPrice(cursor.getInt(cursor.getColumnIndex("price")));
			item.setPriceUnit(cursor.getString(cursor
					.getColumnIndex("priceUnit")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
		}
		cursor.close();
		return item;
	}
	
	public boolean saveNewHouse(NewhouseList item) {
		ContentValues value = new ContentValues();
		value.put("_id", item.getId());
		value.put("name", item.getName());
		value.put("address", item.getAddress());
		value.put("logo", item.getLogo());
		value.put("status", item.getStatus());
		value.put("attr", item.getAttr());
		value.put("price", item.getPrice());
		value.put("priceUnit", item.getPriceUnit());
		value.put("lon", item.getLon());
		value.put("lat", item.getLat());
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("time", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(
				AppDataConfig.CallHistoryTables[1], value);
		return flag > 0 ? true : false;
	}

	public List<RenthouseList> getRentHouseList() {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[2],"time","desc");
		List<RenthouseList> list = new ArrayList<RenthouseList>();
		while (cursor.moveToNext()) {
			RenthouseList item = new RenthouseList();
			item.setID(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStyle(cursor.getString(cursor.getColumnIndex("style")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setPriceUnit(cursor.getString(cursor
					.getColumnIndex("priceUnit")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
			list.add(item);
		}
		cursor.close();
		return list;
	}
	
	public RenthouseList getRentHouseById(String id) {
		Cursor cursor = DBHelper.getInstance(context).query(
				AppDataConfig.CallHistoryTables[2],id);
		RenthouseList item=null;
		if (cursor.moveToNext()) {
			item = new RenthouseList();
			item.setID(cursor.getInt(cursor.getColumnIndex("_id")));
			item.setName(cursor.getString(cursor.getColumnIndex("name")));
			item.setAddress(cursor.getString(cursor.getColumnIndex("address")));
			item.setLogo(cursor.getString(cursor.getColumnIndex("logo")));
			item.setStyle(cursor.getString(cursor.getColumnIndex("style")));
			item.setPrice(cursor.getString(cursor.getColumnIndex("price")));
			item.setPriceUnit(cursor.getString(cursor
					.getColumnIndex("priceUnit")));
			item.setLon(cursor.getDouble(cursor.getColumnIndex("lon")));
			item.setLat(cursor.getDouble(cursor.getColumnIndex("lat")));
			item.setTime(cursor.getString(cursor.getColumnIndex("time")));
		}
		cursor.close();
		return item;
	}
	
	public boolean saveRentHouse(RenthouseList item) {
		ContentValues value = new ContentValues();
		value.put("_id", item.getID());
		value.put("name", item.getName());
		value.put("address", item.getAddress());
		value.put("logo", item.getLogo());
		value.put("style", item.getStyle());
		value.put("price", item.getPrice());
		value.put("priceUnit", item.getPriceUnit());
		value.put("lon", item.getLon());
		value.put("lat", item.getLat());

		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");// 设置日期格式
		value.put("time", df.format(new Date()));// 获取当前时间
		long flag = DBHelper.getInstance(context).save(
				AppDataConfig.CallHistoryTables[2], value);
		return flag > 0 ? true : false;
	}

}
